/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
 *
 * @author Eduardo
 */
public class JConexion {

//    Connection conn = null;
//    Statement stat = null;
//    ResultSet rs = null;
//    String path = "";
//    String accion = "";
//    String query = "";
//
//    public JConexion(String path, String accion, String query) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
//
//        this.path = path;
//        this.accion = accion;
//        this.query = query;
//
//        //DriverManager.getConnection("jdbc:mysql//localhost:3306/royal_mowes", "root", "adminadmin");
//
//        if(this.path.equals("MySQL")){
//            Class.forName("com.mysql.jdbc.Driver").newInstance();
//            conn = DriverManager.getConnection("jdbc:mysql//localhost:3306/rowal_mowes", "root", "adminadmin");
//            stat = conn.createStatement();
//            if(this.accion.equals("Consulta")){
//                rs = stat.executeQuery(query);
//            }else if(this.accion.equals("Editar")){
//                stat.executeUpdate(query);
//            }
//        }
//
//
//    }


    String Path,Accion,Query,res,Salida;
    String s[];
    ArrayList list = new ArrayList();
    int Header=0;
    String DNS ="";
    String User="";
    String Pass="";
    String path="";
        //int j=0;
        //String [] Salida;

    public JConexion(String path){
        this.Path=path;

        if(Path.equals("MySQLRM")){
            this.DNS="jdbc:mysql://localhost:3306/royal_mowes";
            this.User="root";
            this.Pass="adminadmin";
        }

    }


    public ArrayList MySQLArray (String Path,String Accion,String Query) throws InstantiationException, IllegalAccessException{
    /*
        DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@WIN01:1521:oracleBD", "user", "passw");         // driver@machineName:port:SID           ,  userid,  password
        Statement stmt = conn.createStatement();
        ResultSet rset =
        stmt.executeQuery("select BANNER from SYS.V_$VERSION");    while (rset.next())         System.out.println (rset.getString(1));   // Print col 1    stmt.close();
    */
        this.Accion=Accion;

        if(Path.equals("MySQLRM")){
            this.DNS="jdbc:mysql://localhost:3306/royal_mowes";
            this.User="root";
            this.Pass="adminadmin";
        }
        try {
            /*** Registro de driver y conexión a base de datos ****/
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
            //Connection con = DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Documents and Settings/dp1om025/My Documents/odbc.MDB", "", "");

            Connection con = DriverManager.getConnection(this.DNS, this.User, this.Pass);


            System.out.println("Conexion con exito");
            //Connection con = DriverManager.getConnection("jdbc:oracle:thin:@172.16.48.97:1521:ORDB", "equintero", "dinora1");

            Statement sentencia = con.createStatement();                                                                                                                                                                                                                                                                                                                                                                                       //C:\Documents and Settings\dp1om025\My Documents

            if (this.Accion.equals("Consulta")){
                ResultSet rs = sentencia.executeQuery( Query );

                ResultSetMetaData rsmd = rs.getMetaData();
                //getRowCount();

                int numberofcolumn = rsmd.getColumnCount ();

                //Salida [] s;
                //s = new String[numberofcolumn];
                System.out.println ("NumeroOfcolumnas" + numberofcolumn);
                                               //int j=0;
                //s = new String[numberofcolumn];
                while ( rs.next()) {
                    int j=0;
                    s = new String[numberofcolumn];
                    //System.out.println(s.length);
                    for (int i=1; i <numberofcolumn+1; i++){
                        s[j] = rs.getString(i);
                        //System.out.println(s.length + "Valor de" + i + "Valor de J" + j);
                        j++;
                    } //for
                    //System.out.println(s.length + "Valor de" + i);
                    list.add(s);
                } //while
            }

            if (this.Accion.equals("Insert")){
                sentencia.executeUpdate( Query);
                Salida ="Insert Con Exito";
            }

            /*** Cerramos ***/
            sentencia.close();
            con.close();
        }
        catch( ClassNotFoundException e ) { e.printStackTrace();  }
        catch (SQLException e) { e.printStackTrace();  }
    return (list);
    }
}


